package com.atguigu.webadmin.service.impl;

import com.atguigu.model.pojo.*;
import com.atguigu.webadmin.mapper.*;
import com.atguigu.webadmin.service.LeaseAgreementService;
import com.atguigu.webadmin.vo.agreement.AgreementQueryVo;
import com.atguigu.webadmin.vo.agreement.AgreementVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @Author 梅超凡
 * @Date 2024/11/12~20:29
 * @Description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
*/
@Service
@Slf4j
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {


    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    /**
     * @Author 梅超凡 
     * @Date 2024/11/15~20:11
     * @Description 从lease_agreement表中选择租赁协议的基本信息，包括ID、电话、姓名、身份证号、租赁起止日期、租金、押金、状态、来源类型、附加信息等。
     * 通过左连接（left join）关联以下表：
     * apartment_info：获取公寓信息，包括公寓ID、名称、区县ID、区县名称、城市ID、城市名称、省份ID、省份名称。
     * room_info：获取房间信息，包括房间ID、房间号。
     * payment_type：获取支付方式信息，包括支付方式ID、名称、支付月数。
     * lease_term：获取租赁期限信息，包括租赁期限ID、月数、单位。
    */
    @Override
    public IPage<AgreementVo> pageAgreementByQuery(IPage<AgreementVo> voIPage, AgreementQueryVo queryVo) {
        IPage<AgreementVo> agreementVoIPage = leaseAgreementMapper.pageAgreementByQuery(voIPage,queryVo);
        log.info("查询结果为：{}",agreementVoIPage);
        return agreementVoIPage;
    }



    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;
    /**
     * @Author 梅超凡
     * @Date 2024/11/15~20:16
     * @Description 通过id查询租约信息
    */
    @Override
    public AgreementVo getAgreementById(Long id) {
        //1.查询租约信息
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);

        //2.查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(leaseAgreement.getApartmentId());

        //3.查询房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(leaseAgreement.getRoomId());

        //4.查询支付方式
        PaymentType paymentType = paymentTypeMapper.selectById(leaseAgreement.getPaymentTypeId());

        //5.查询租期
        LeaseTerm leaseTerm = leaseTermMapper.selectById(leaseAgreement.getLeaseTermId());

        AgreementVo adminAgreementVo = new AgreementVo();
        //属性对拷
        BeanUtils.copyProperties(leaseAgreement, adminAgreementVo);
        adminAgreementVo.setApartmentInfo(apartmentInfo);
        adminAgreementVo.setRoomInfo(roomInfo);
        adminAgreementVo.setPaymentType(paymentType);
        adminAgreementVo.setLeaseTerm(leaseTerm);
        return adminAgreementVo;
    }
}




